<!DOCTYPE html>
<html lang="zh-cn">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>登录</title>

  <!-- 导入jquery -->
  <script src="./libs/jquery-1.12.4.min.js"></script>
  <!-- 导入bootstrap -->
  <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
  <script src="./libs/bootstrap/js/bootstrap.min.js"></script>
  <!-- 导入art-template -->
  <script src="./libs/template-web.js"></script>
  <!-- 导入md5 -->
  <script src="./libs/md5.min.js"></script>
  <style>
    /* 登陆框上margin */
    .my-panel {
      margin-top: 200px;
    }

    .btn-width {
      width: 200px;
    }
  </style>
</head>

<body>
  <!-- bootstrap的容器  带版心的-->
  <div class="container">
    <!-- 栅格的行 -->
    <div class="row">
      <!-- 栅格的列 offset 列偏移 -->
      <div class="col-xs-6 col-xs-offset-3">
        <!-- 面板 -->
        <div class="panel panel-default panel-primary my-panel">
          <!-- 面板的标题 -->
          <div class="panel-heading">
            <h3 class="panel-title">登录CQ管理系统</h3>
          </div>
          <!-- 面板的内容 -->
          <div class="panel-body">
            <!-- 表单 -->
            <form class="form-horizontal" id="form">
              <div class="form-group">
                <label for="userName" class="col-sm-2 control-label">用户名</label>
                <div class="col-sm-10">
                  <input type="text" name="username" class="form-control" id="username" placeholder="王**">
                </div>
              </div>
              <div class="form-group">
                <label for="passWord" class="col-sm-2 control-label">密&nbsp;&nbsp;&nbsp;&nbsp;码</label>
                <div class="col-sm-10">
                  <input type="password" name="password" class="form-control" id="password" placeholder="密码">
                </div>
              </div>
              <div class="col-sm-offset-2 col-sm-10">
                <input type="submit" class="btn btn-success btn-width btn-login" value="登录">
                <a href="./register.html"><button type="button" class="btn btn-danger btn-width">注册</button></a>
              </div>
            </form>
          </div>

        </div>
      </div>
    </div>
  </div>

  <script>
    $(function () {
      $('.btn-login').on('click', function (e) {
        e.preventDefault()
        let username = $('#username').val().trim()
        let password = $('#password').val().trim()
        password = md5(password, 'scuretkey')
        $('#password').val(password)

        if (username == '' || password == '') {
          alert('账号或密码不能为空')
          return
        }
        $.ajax({
          type: 'post',
          url: 'http://127.0.0.1:3000/user/login',
          data: {
            username,
            password
          },
          success: function (backData) {
            if (backData.code == 200) {
              alert('登录成功')
              window.location.href = './index.html'
            } else {
              console.log(backData);
            }
          }
        })


      })
    })
  </script>
</body>

</html>